package com.lbs.jiepiaisi.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.activity.NewsDetailActivity;
import com.lbs.jiepiaisi.adapter.NewsListAdapter;
import com.lbs.jiepiaisi.bean.NewsEntity;
import com.lbs.jiepiaisi.biz.NewsBiz;
import com.lbs.jiepiaisi.parser.NewsParser;
import com.lbs.jiepiaisi.utils.ACache;
import com.lbs.jiepiaisi.utils.ButtonUtil;
import com.lbs.jiepiaisi.utils.Constant;

/**
 *
 * 时间：2018/1/12 15:25
 * 作用：
 */

public class NewsListFragment extends Fragment {
    private String  type;
    public static final String NEWS_TYPE = "new_type";

    ACache aCache;

    NewsReceiver mNewsReceiver;


    private ListView mNewsList;
    public static NewsListFragment getInstance(String type){
        Bundle args = new Bundle();
        args.putString(NEWS_TYPE, type);
        NewsListFragment pageFragment = new NewsListFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getString(NEWS_TYPE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_list, container, false);
        mNewsList = (ListView) view.findViewById(R.id.lv_news_list);
        mNewsReceiver = new NewsReceiver();
        getActivity().getApplicationContext().registerReceiver(mNewsReceiver,new IntentFilter(type));
        getExtras();
        return view;
    }

    private void getExtras(){
        aCache =  ACache.get(getActivity());
        String newsString = aCache.getAsString(NEWS_TYPE + type);
        if(newsString != null){
           mNewsList.setAdapter(new NewsListAdapter(getActivity(),NewsParser.parser(newsString).getResult().getData()));
        }
        NewsBiz newsBiz = new NewsBiz();
        newsBiz.getNews(type,type);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().getApplicationContext().unregisterReceiver(mNewsReceiver);
    }

    //接收实时跟踪数据的广播
    class NewsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final NewsEntity newsEntity =(NewsEntity) intent.
                    getSerializableExtra(Constant.KEY_DATA);
            if(newsEntity != null && newsEntity.getResult() != null){
                aCache.put(NEWS_TYPE + type,new Gson().toJson(newsEntity),60);
                NewsListAdapter newsListAdapter =  new NewsListAdapter(getActivity(),newsEntity.getResult().getData());
                mNewsList.setAdapter(newsListAdapter);
                mNewsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if(ButtonUtil.isFastClick()){
                            Intent intent = new Intent(getActivity(),NewsDetailActivity.class);
                            intent.putExtra("news", newsEntity.getResult().getData().get(position));
                            startActivity(intent);
                        }
                    }
                });
            }
        }
    }
}
